import SimpleITK as sitk
from skimage.io import imread
from skimage.color import rgb2gray
import matplotlib.pylab as plt


def show_image(img, title=None):
    nda = sitk.GetArrayViewFromImage(img)
    plt.imshow(nda, cmap='gray')
    plt.axis('off')
    if (title):
        plt.title(title, size=20)


IM_PATH = "../experiment_fig/brain1.png"
# img = 255*rgb2gray(imread('brain.png'))
img = 255 * rgb2gray(imread(IM_PATH))
img_T1 = sitk.GetImageFromArray(img)
img_T1_255 = sitk.Cast(sitk.RescaleIntensity(img_T1), sitk.sitkUInt8)

seed = (205, 221)
for upper in [80, 85, 90]:
    plt.figure(figsize=(18, 20))
    plt.subplot(221)
    show_image(img_T1, "Original Image")
    plt.scatter(seed[0], seed[1], color='red', s=50)
    plt.subplot(222)
    seg = sitk.ConnectedThreshold(img_T1, seedList=[seed], lower=40, upper=upper, replaceValue=255)
    show_image(seg, "Region Growing")
    plt.subplot(223)
    show_image(sitk.LabelOverlay(img_T1_255, seg), "Connected Region")
    plt.axis('off')
    plt.tight_layout()
    plt.show()

    # ConfidenceConnected
